/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

/**
 * Marquee text widget.
 * <p>
 * The width of the marquee needs to be specified along with the text. The
 * direction of the marquee can also be set. With the correct values of stepping
 * and stepping intervals we could achieve a good smooth scrolling
 * 
 * </p>
 * 
 * @author kentelt
 * 
 */
public final class MarqueeText extends TextEffect {

	/**
	 * Direction of the marquee
	 * <p>
	 * 
	 * @see MarqueeText#setDirection(int) </p>
	 */
	public static final int LEFT_TO_RIGHT = 10;
	/**
	 * Direction of the marquee
	 * <p>
	 * 
	 * @see MarqueeText#setDirection(int) </p>
	 */
	public static final int RIGHT_TO_LEFT = 20;

	private int direction = RIGHT_TO_LEFT;

	private volatile int step = 5;

	private int stringWidth;

	private volatile int txtXpos;

	public MarqueeText(Theme theme, String text) {
		this(theme, text, 50);
	}

	/**
	 * 
	 * @param text
	 * @param width
	 *            of the marquee sign
	 */
	public MarqueeText(Theme theme, String text, int width) {
		super(theme, width, 0);
		this.text = text;
		updateModel();
	}

	protected void doAniTask() {
		if (direction == LEFT_TO_RIGHT) {
			txtXpos += step;
			if (txtXpos > width) {
				txtXpos = -stringWidth;
			}
		} else {
			txtXpos -= step;
			if (txtXpos < -stringWidth) {
				txtXpos = width;
			}
		}
	}

	public final String getText() {
		return text;
	}

	public final void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, super.width, super.height, true);
		g.setColor(theme.fgColor);
		if (theme.bmFont != null) {
			theme.bmFont.drawString(g, text, txtXpos, 0, 0);
		} else {
			g.setFont(theme.widgetFont);
			g.drawString(text, txtXpos, 0, 0);
		}
		release(g, width, height, true);

	}

	/**
	 * reset the marquee text position
	 */
	public void resetTextPosition() {
		txtXpos = 0;
		reDrawWidget();
	}

	/**
	 * Set the marquee direction.
	 * 
	 * @see MarqueeText#LEFT_TO_RIGHT
	 * @see MarqueeText#RIGHT_TO_LEFT
	 * @param direction
	 *            of the Marquee. The default is LEFT_TO_RIGHT
	 */
	public final void setDirection(int direction) {
		this.direction = direction;
		updateModel();
	}

	public final void setText(String text) {
		this.text = text;
		updateModel();
		reDraw();
		stateChanged(this);
	}

	/**
	 * Number of pixels to step forward/backward for each tick
	 * 
	 * @param millisec
	 *            default is 500ms
	 */
	public final void setTickStep(int millisec) {
		this.step = millisec;
	}

	protected void updateModel() {
		if (theme.bmFont != null) {
			super.height = theme.bmFont.getHeight();
		} else {

			super.height = theme.widgetFont.getHeight();
		}
		if (direction == RIGHT_TO_LEFT) {
			txtXpos = super.width;
		} else {
			txtXpos = 0;
		}
		stringWidth = theme.widgetFont.stringWidth(text);
	}

}
